// pages/handle/pages/choose/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [ //数据
            {
                letter: "C",
                data: [
                    {
                        id: 1,
                        title: "出生公证",
                        certified_price: "100", //认证价
                        cost_price: "10.00", //工本价
                        checked: false, //是否选中
                        quantity: 1, //默认数
                    }
                ]
            },
            {
                letter: "Q",
                data: [{
                    id: 2,
                    title: "亲属关系公证",
                    checked: false, //是否选中
                    certified_price: "100", //认证价
                    cost_price: "10.00", //工本价
                    checked: false, //是否选中
                    quantity: 1, //默认数
                }]
            },
            {
                letter: "W",
                data: [{
                    id: 0,
                    title: "委托/声明公证",
                    checked: false, //是否选中
                    certified_price: "100", //认证价
                    cost_price: "10.00", //工本价
                    checked: false, //是否选中
                    quantity: 1, //默认数
                }]
            },
        ],
        info_list: [],
        disabled: false,
        touchmove: false, // 是否在索引表上滑动
        touchmoveIndex: -1,
        titleHeight: 0, // A字距离窗口顶部的高度
        indexBarHeight: 0, // 索引表高度
        indexBarItemHeight: 0, // 索引表子项的高度
        winHeight: 0,
        scrollTop: 0,
        cart: [], //购物车
        c_index: '', //一级索引
        c_index2: '', //二级索引
        c_id2: '', //选中二级id
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const that = this;
        setTimeout(() => {
            wx.getSystemInfo({
                success: function (res) {
                    let winHeight = res.windowHeight
                    let barHeight = winHeight - res.windowWidth / 750 * 232
                    that.setData({
                        winHeight: winHeight,
                        indexBarHeight: barHeight,
                        indexBarItemHeight: barHeight / 25,
                        titleHeight: res.windowWidth / 750 * 132,
                        info_list: that.data.list
                    })
                }
            })
        }, 10)
    },
    touchStart(e) {
        this.setData({
            touchmove: true
        })
        let pageY = e.touches[0].pageY - this.data.scrollTop;
        let index = Math.floor((pageY - this.data.titleHeight) / this.data.indexBarItemHeight)
        let item = this.data.info_list[index]
        if (item) {
            this.setData({
                scrollViewId: item.letter,
                touchmoveIndex: index
            })
            wx.pageScrollTo({
                scrollTop: this.data.info_list[index].stickyTop + 1,
                duration: 0
            })
        }
    },
    touchMove(e) {
        let pageY = e.touches[0].pageY - this.data.scrollTop;
        let index = Math.floor((pageY - this.data.titleHeight) / this.data.indexBarItemHeight)
        let item = this.data.info_list[index]
        if (item) {
            this.setData({
                scrollViewId: item.letter,
                touchmoveIndex: index
            })
            wx.pageScrollTo({
                scrollTop: this.data.info_list[index].stickyTop + 1,
                duration: 0
            })
        }
    },
    touchEnd() {
        this.setData({
            touchmove: false,
            touchmoveIndex: -1
        })
    },
    touchCancel() {
        this.setData({
            touchmove: false,
            touchmoveIndex: -1
        })
    },
    stickyChange: function (e) {
        let index = e.detail.index;
        let key = `info_list[${index}].stickyTop`
        this.setData({
            [key]: e.detail.top
        })
    },
    //页面滚动执行方式
    onPageScroll(e) {
        this.setData({
            scrollTop: e.scrollTop
        })
    },
    // 处理单选按钮
    to_select: function (e) {
        var that = this;
        let disabled = that.data.disabled;
        let index = e.currentTarget.dataset.index; //一级索引
        let level = e.currentTarget.dataset.level; //二级索引
        let id = e.currentTarget.dataset.id; //对应的id
        console.log(index, level, id)
        let vals = [...this.data.info_list]
        if (disabled) {
            return false
        }
        vals.forEach((item, idx) => {
            item.data.forEach((items, idxs) => {
                if (items.id === id) {
                    items.checked = true
                } else {
                    items.checked = false
                }
            })
        })
        that.setData({
            c_index: index, //一级索引
            c_index2: level, //二级索引
            c_id2: id, //二级id
            info_list: vals
        })
        let cart_data = that.data.info_list[index].data[level];
        cart_data.index = index;
        cart_data.level = level;
        //单选购物车只能有一个
        if (this.data.cart.length > 0) {
            let cart = that.data.cart;
            cart.splice(0, 1);
            this.setData({
                cart: cart
            })
            setTimeout(() => {
                that.add_cart(cart_data)
            }, 50)
        } else {
            that.add_cart(cart_data)
        }
    },
    //加
    to_add: function (e) {
        var that = this;
        let index = e.currentTarget.dataset.index; //一级索引
        let level = e.currentTarget.dataset.level; //二级索引
        let id = e.currentTarget.dataset.id; //对应的产品id
        var info = that.data.info_list;
        var quantity = info[index].data[level].quantity; //当前数量
        var certified_price = info[index].data[level].certified_price; //认证价
        var cost_price = info[index].data[level].cost_price; //工本价
        let value = `info_list[${index}].data[${level}].quantity`; //当前大分类下的选中数量
        this.setData({
            [value]: quantity + 1,
        })
        let cart_data = this.data.info_list[index].data[level];
        cart_data.index = index;
        cart_data.level = level;
        cart_data.amount = (Number(certified_price) + Number((quantity + 1) * cost_price)).toFixed(2),
            this.add_cart(cart_data)
    },
    //购物车 添加
    add_cart(data) {
        let cart = this.data.cart
        if (cart.length == 0) {
            cart.push(data)
        } else {
            let index = -1;
            for (let i = 0; i < cart.length; i++) {
                if (cart[i].id == data.id) {
                    index = i;
                    break
                }
            }
            if (index >= 0) {

            } else {
                cart.push(data)
            }
        }
        this.setData({
            cart: cart
        })
        console.log('购物车：', this.data.cart)
    },
    //购物车 移除
    reduce_cart(data) {
        var that = this;
        let cart = that.data.cart;
        if (cart.length == 0) {
            console.log('无选中')
        } else {
            let index = '';
            for (let i = 0; i < cart.length; i++) {
                if (cart[i].id == data.id) {
                    index = i;
                    break
                }
            }
            if (index >= 0) {
                if (cart[index].quantity > 0) {
                    cart[index].quantity == cart[index].quantity - 1
                    console.log('减少')
                } else {
                    cart.splice(index, 1);
                    console.log('移除')
                }
            }
        }
        this.setData({
            cart: cart
        })
    },
    //减
    to_reduce: function (e) {
        var that = this;
        let index = e.currentTarget.dataset.index; //一级索引
        let level = e.currentTarget.dataset.level; //二级索引
        let id = e.currentTarget.dataset.id; //对应的产品id
        var info = that.data.info_list;
        var certified_price = info[index].data[level].certified_price; //认证价
        var cost_price = info[index].data[level].cost_price; //工本价
        var quantity = info[index].data[level].quantity;
        if (quantity == 1) {
            return;
        }
        let value = `info_list[${index}].data[${level}].quantity`; //当前大分类下的选中数量
        this.setData({
            [value]: quantity - 1,
        })
        let cart_data = this.data.info_list[index].data[level];
        cart_data.index = index;
        cart_data.level = level;
        cart_data.amount = (Number(certified_price) + Number((quantity - 1) * cost_price)).toFixed(2),
            this.reduce_cart(cart_data)
    },
    //计算总金额
    change_total(info) {
        var total = 0;
        var nums = 0;
    },
    //下一步
    next_step: function (e) {
        let cart = this.data.cart;
        let cd_id = this.data.c_id2; //选中的id
    
        if (cart.length <= 0) {
            wx.showToast({
                title: '请选择公证事项',
                icon: 'none',
                mask: true,
                duration: 1500
            });
        } else {
            this.setData({
                disabled: true,
            })
            //pages/material/pages/basis/form_a/index //委托公证
            //pages/material/pages/basis/form_b/index //出生
            //pages/material/pages/basis/form_c/index //亲属
            let data = this.data.cart
            
            //根据选中产品id 判断跳转路径
            if (data[0].id==0) {
                wx.redirectTo({
                    url: '/pages/material/pages/basis/form_a/index?base_type='+data[0].id+'&number_goods='+data[0].quantity,
                })
            } else if (data[0].id==1) {
                wx.redirectTo({
                    url: '/pages/material/pages/basis/form_b/index?base_type='+data[0].id+'&number_goods='+data[0].quantity,
                })
            } else {
                wx.redirectTo({
                    url: '/pages/material/pages/basis/form_c/index?base_type='+data[0].id+'&number_goods='+data[0].quantity,
                })
            }
            
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})